#!/usr/bin/env python3
# $Id: test3-1.py 7c6bb3d1dea5 2012/08/26 01:44:08 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: 7c6bb3d1dea5 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-08-26 01:44:08Z $"

import optparse
import os
import sys

from venya3.lib import cmdparser
from venya3.lib import ip
from venya3.lib import ipv6
from venya3.lib import stdlib

class rfc4193(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-p", "--prefix", action = "store_true",
            help = "Generate prefix, not just the IPv6 node address",
            dest = "prefix"),
         optparse.make_option("-l", "--length", type = "int",
            help = "Length of the IPv6 address/prefix", dest = "length"),
   ) + cmdparser.GetOpt.option_list

   def __init__(self, **Keywords):
      super(rfc4193, self).__init__(**Keywords)

   def handle(self):
      (options, args) = self.parse()

      address = None
      try:
         cls = ipv6.rfc4193(options.length, options.prefix)
         address = cls.GetIPv6()
      except ipv6.RFCError as err:
         self.error(err.message)

      print(address)

# If the namespace is main
if __name__ == "__main__":
   rfcopt = None
   try:
      # Construct the RFC 4193 tool command line options
      rfcopt = rfc4193(version = 1.3, args = sys.argv)
      # If we have few parameter, then print help and exit
      if len(rfcopt) <= 1:
         rfcopt.printhelp()
      else:
         # If we have correct number of arguments, call the default handler
         # (handle()) function, any errors will be GetOptError,
         # print the message
         # and exits with code 1
         rfcopt.handle()
   except cmdparser.GetOptError as err:
      print(err)
      sys.exit(1)
